package com.zhaopin.qa.jmeter.plugins.multienvuser.controller;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JOptionPane;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.GuiUtils;
import org.apache.jorphan.gui.ObjectTableModel;
import org.apache.jorphan.reflect.Functor;

import com.zhaopin.qa.jmeter.plugins.multienvuser.config.EnvArgument;

import sun.swing.table.DefaultTableCellHeaderRenderer;

class MultiEnvUserArgumentsPanel extends ArgumentsPanel {

	private static final long serialVersionUID = -5138184859226073758L;

	private String name;

	public MultiEnvUserArgumentsPanel(String name) {
		super(genLableName(name), null, true, false);
		super.getTable().getTableHeader().setDefaultRenderer(new DefaultTableCellHeaderRenderer());
		this.name = name;
	}

	public String getEnvOrUserName() {
		return name;
	}

	@Override
	protected void initializeTableModel() {
		if (tableModel == null) {
			tableModel = new ObjectTableModel(// $NON-NLS-1$
					new String[] { // $NON-NLS-1$
							"名称", // $NON-NLS-1$
							"值", // $NON-NLS-1$
							"描述"// $NON-NLS-1$
					}, // $NON-NLS-1$
					EnvArgument.class, // $NON-NLS-1$
					new Functor[] { // $NON-NLS-1$
							new Functor("getName"), //$NON-NLS-1$
							new Functor("getValue"), //$NON-NLS-1$
							new Functor("getDescription")//$NON-NLS-1$
					}, // $NON-NLS-1$
					new Functor[] { // $NON-NLS-1$
							new Functor("setName"), //$NON-NLS-1$
							new Functor("setValue"), //$NON-NLS-1$
							new Functor("setDescription") }, //$NON-NLS-1$
					new Class[] { // $NON-NLS-1$
							String.class, // $NON-NLS-1$
							String.class, // $NON-NLS-1$
							String.class// $NON-NLS-1$
					});
		}
	}

	private static String genLableName(String name) {
		return new StringBuffer("设置").append(name.split("\\.")[1]).append("用户参数").toString();
	}

	@Override
	protected EnvArgument makeNewArgument() {
		return new EnvArgument("", "");
	}

	@Override
	public TestElement createTestElement() {
		Arguments args = new Arguments();
		modifyTestElement(args);

		args.setName("EnvUserArgs.user." + name);

		return args;
	}

	/**
	 * Add values from the clipboard
	 */
	@Override
	protected void addFromClipboard() {
		GuiUtils.stopTableEditing(getTable());
		int rowCount = getTable().getRowCount();
		try {
			String clipboardContent = GuiUtils.getPastedText();
			if (clipboardContent == null) {
				return;
			}
			String[] clipboardLines = clipboardContent.split("\n");
			for (String clipboardLine : clipboardLines) {
				String[] clipboardCols = clipboardLine.split("\t");
				if (clipboardCols.length > 0) {
					EnvArgument argument = makeNewArgument();
					argument.setName(clipboardCols[0]);
					if (clipboardCols.length > 1) {
						argument.setValue(clipboardCols[1]);
						if (clipboardCols.length > 2) {
							argument.setDescription(clipboardCols[2]);
							if (clipboardCols.length > 3) {
								argument.setGlobal(Boolean.valueOf(clipboardCols[3]));
							}
						}
					}
					tableModel.addRow(argument);
				}
			}
			if (getTable().getRowCount() > rowCount) {
				// Enable DELETE (which may already be enabled, but it won't hurt)
				getDeleteButton().setEnabled(true);

				// Highlight (select) the appropriate rows.
				int rowToSelect = tableModel.getRowCount() - 1;
				getTable().setRowSelectionInterval(rowCount, rowToSelect);
			}
		} catch (IOException ioe) {
			JOptionPane.showMessageDialog(this,
					"Could not add read arguments from clipboard:\n" + ioe.getLocalizedMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		} catch (UnsupportedFlavorException ufe) {
			JOptionPane
					.showMessageDialog(this,
							"Could not add retrieve " + DataFlavor.stringFlavor.getHumanPresentableName()
									+ " from clipboard" + ufe.getLocalizedMessage(),
							"Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}
